#include "ObjectPool.h"
#include "ConCurrentAlloc.h"

void Alloc1()
{
	for (size_t i = 0; i < 5; i++)
	{
		void* ptr = ConcurrentAlloc(6);
	}
}

void Alloc2()
{
	for (size_t i = 0; i < 5; i++)
	{
		void* ptr = ConcurrentAlloc(7);
	}
}

void TLSTest()
{
	std::thread t1(Alloc1);
	std::thread t2(Alloc2);

	t1.join();
	t2.join();
}

void TestConcurrentAlloc1()
{
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(28);
	void* p4 = ConcurrentAlloc(7);
	void* p5 = ConcurrentAlloc(15);
	void* p6 = ConcurrentAlloc(3);
	void* p7 = ConcurrentAlloc(6);
	/*void* p8 = ConcurrentAlloc(2);
	void* p9 = ConcurrentAlloc(1);
	void* p10 = ConcurrentAlloc(4);*/
	
	cout << p1 << endl;
	cout << p2 << endl;
	cout << p3 << endl;
	cout << p4 << endl;
	cout << p5 << endl;
	/*cout << p6 << endl;
	cout << p7 << endl;
	cout << p8 << endl;
	cout << p9 << endl;
	cout << p10 << endl;*/

	
	ConcurrentFree(p1);
	ConcurrentFree(p2);
	ConcurrentFree(p3);
	ConcurrentFree(p4);
	ConcurrentFree(p5);
	ConcurrentFree(p6);
	ConcurrentFree(p7);
}

void TestConcurrentAlloc2()
{
	for (size_t i = 0; i < 5; ++i)
	{
		void* p1 = ConcurrentAlloc(6);
		cout << p1 << endl;
	}
	void* p2 = ConcurrentAlloc(8);
	cout << p2 << endl;
}

void TestFreeLists1()
{
	void* freeLists = nullptr;
	void* obj = (void*)malloc(12);
	printf("0x%p\n", freeLists);
	printf("0x%p\n", obj);
	printf("0x%p\n\n", *((void**)obj));

	printf("0x%p\n", &freeLists);
	printf("0x%p\n", &obj);
	printf("0x%p\n\n", *((void**)obj));

	*((void**)obj) = freeLists;
	printf("0x%p\n", &obj);
	printf("0x%p\n", &freeLists);
	printf("0x%p\n\n", *((void**)obj));


	freeLists = obj;

}

void MultiAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void MultiAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(16);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void TestMultiThread()
{
	std::thread t1(MultiAlloc1);
	std::thread t2(MultiAlloc2);

	t1.join();
	t2.join();
}

void BigAlloc()
{
	void* p1 = ConcurrentAlloc(257 * 1024);
	ConcurrentFree(p1);

	void* p2 = ConcurrentAlloc(129 * 8 * 1024);
	ConcurrentFree(p2);
}

int main22()
{
	//TestObjectPool();
	//TLSTest();
	TestConcurrentAlloc1();
	//TestConcurrentAlloc2();
	//TestMultiThread();

	//cout << sizeof(Span) << endl;
	//ObjectPoolModule::TestObjectPool();
	//BigAlloc();
	//TestFreeLists1();

	return 0;
}


void TestFreeLists()
{
	int x = 0x1000;
	int* px = &x;
	int* tx = px;
	int** ppx = &px;
	long long y = (long long)(*(int**)px);
	//printf("0x%p\n", *(int*)x);
	//printf("0x%p\n", y);
	printf("0x%p\n", &x);
	printf("0x%p\n", &px);
	printf("0x%p\n", px);
	printf("0x%p\n", tx);
	printf("0x%p\n", ppx);
	printf("0x%p\n", *(int**)px);
}